{% extends "base.html" %}

{% block title %}Edit Activity - Volunteer Platform{% endblock %}

{% block content %}
<div class="container mt-4">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <div class="card">
                <div class="card-body">
                    <h2 class="card-title mb-4">Edit Activity</h2>
                    
                    <form method="POST" action="{{ url_for('organization_edit_activity', activity_id=activity.id) }}">
                        <div class="mb-3">
                            <label for="title" class="form-label">Activity Title</label>
                            <input type="text" class="form-control" id="title" name="title" 
                                   value="{{ activity.title }}" required>
                        </div>
                        
                        <div class="mb-3">
                            <label for="description" class="form-label">Activity Description</label>
                            <textarea class="form-control" id="description" name="description" 
                                      rows="5" required>{{ activity.description }}</textarea>
                        </div>
                        
                        <div class="mb-3">
                            <label for="category" class="form-label">Category</label>
                            <select class="form-select" id="category" name="category" required>
                                <option value="">Select Category</option>
                                <option value="education" {% if activity.category == 'education' %}selected{% endif %}>Education</option>
                                <option value="environment" {% if activity.category == 'environment' %}selected{% endif %}>Environment</option>
                                <option value="healthcare" {% if activity.category == 'healthcare' %}selected{% endif %}>Healthcare</option>
                                <option value="elderly" {% if activity.category == 'elderly' %}selected{% endif %}>Elderly Care</option>
                                <option value="other" {% if activity.category == 'other' %}selected{% endif %}>Other</option>
                            </select>
                        </div>
                        
                        <div class="mb-3">
                            <label for="location" class="form-label">Location</label>
                            <input type="text" class="form-control" id="location" name="location" 
                                   value="{{ activity.location }}" required>
                        </div>
                        
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="start_date" class="form-label">Start Time</label>
                                <input type="datetime-local" class="form-control" id="start_date" name="start_date" 
                                       value="{{ activity.start_date.strftime('%Y-%m-%dT%H:%M') }}" required>
                            </div>
                            
                            <div class="col-md-6 mb-3">
                                <label for="end_date" class="form-label">End Time</label>
                                <input type="datetime-local" class="form-control" id="end_date" name="end_date" 
                                       value="{{ activity.end_date.strftime('%Y-%m-%dT%H:%M') }}" required>
                            </div>
                        </div>
                        
                        <div class="mb-3">
                            <label for="max_participants" class="form-label">Maximum Participants</label>
                            <input type="number" class="form-control" id="max_participants" name="max_participants" 
                                   value="{{ activity.max_participants }}" min="1" required>
                            {% if activity.current_participants > 0 %}
                            <small class="text-muted">Currently {{ activity.current_participants }} people registered</small>
                            {% endif %}
                        </div>
                        
                        <div class="alert alert-info">
                            <i class="fas fa-info-circle"></i> 
                            {% if activity.status == 'approved' %}
                            Editing an approved activity does not require re-approval.
                            {% else %}
                            Editing a pending activity requires re-approval.
                            {% endif %}
                        </div>
                        
                        <div class="d-grid gap-2">
                            <button type="submit" class="btn btn-primary">Save Changes</button>
                            <a href="{{ url_for('activity_detail', activity_id=activity.id) }}" 
                               class="btn btn-outline-secondary">Back to Activity Details</a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Ensure end time is not earlier than start time
    document.getElementById('start_date').addEventListener('change', function() {
        document.getElementById('end_date').min = this.value;
    });
    
    // If activity has registrations, limit minimum participants to current registration count
    {% if activity.current_participants > 0 %}
    document.getElementById('max_participants').min = {{ activity.current_participants }};
    {% endif %}
});
</script>
{% endblock %} 